/**
 * Application Application.java
 */

package CCT220_Aula20_Exercicio01_13106;


import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;


/**
 * </br>******************************
 * </br>Project: CCT220
 * </br>Package: CCT220_Aula20_Exercicio01_13106
 * </br>Class: Application
 * </br>Last Change: 01/11/2011 16:05:23 by marcel
 * </br>Description:
 * </br>******************************</br>
 * 
 * @author marcel
 * @author 13106
 * 
 */
public class Application {

	private JLabel  jlResultado;

	private JPanel  jpButtons;

	private JButton jbReset;

	private String  conta  = "";
	private String  v1     = "";
	private String  v2     = "";
	private String  op     = "";
	private String  result = "";


	public Application() {

		JFrame jf = new JFrame("Marcel M. Grilo - 13106");
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setSize(250, 300);
		jf.setLayout(new BorderLayout(5, 5));
		this.startComponents();
		jf.add(jlResultado, BorderLayout.NORTH);
		jf.add(jpButtons, BorderLayout.CENTER);
		jf.add(jbReset, BorderLayout.SOUTH);

		jf.setVisible(true);

	}


	private void startComponents() {

		JButton[][] jbtns = new JButton[4][4];
		jbtns[0][0] = new JButton("7");
		jbtns[0][1] = new JButton("8");
		jbtns[0][2] = new JButton("9");
		jbtns[0][3] = new JButton("//");

		jbtns[1][0] = new JButton("4");
		jbtns[1][1] = new JButton("5");
		jbtns[1][2] = new JButton("6");
		jbtns[1][3] = new JButton("*");

		jbtns[2][0] = new JButton("1");
		jbtns[2][1] = new JButton("2");
		jbtns[2][2] = new JButton("3");
		jbtns[2][3] = new JButton("-");

		jbtns[3][0] = new JButton("0");
		jbtns[3][1] = new JButton(".");
		jbtns[3][2] = new JButton("=");
		jbtns[3][3] = new JButton("+");

		jbtns[0][0].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

				conta += "7";
				jlResultado.setText(conta);
			}
		});
		jbtns[0][1].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

				conta += "8";
				jlResultado.setText(conta);

			}
		});
		jbtns[0][2].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

				conta += "9";
				jlResultado.setText(conta);

			}
		});
		// op /
		jbtns[0][3].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

				opEval(3);
			}
		});

		jbtns[1][0].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

				conta += "4";
				jlResultado.setText(conta);

			}
		});
		jbtns[1][1].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

				conta += "5";
				jlResultado.setText(conta);

			}
		});
		jbtns[1][2].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

				conta += "6";
				jlResultado.setText(conta);

			}
		});
		// op *
		jbtns[1][3].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

				opEval(2);
			}
		});

		jbtns[2][0].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

				conta += "1";
				jlResultado.setText(conta);

			}
		});
		jbtns[2][1].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

				conta += "2";
				jlResultado.setText(conta);

			}
		});
		jbtns[2][2].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

				conta += "3";
				jlResultado.setText(conta);

			}
		});
		// op -
		jbtns[2][3].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

				opEval(1);

			}
		});

		jbtns[3][0].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

				conta += "0";
				jlResultado.setText(conta);

			}
		});
		jbtns[3][1].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

				conta += ".";
				jlResultado.setText(conta);

			}
		});
		jbtns[3][2].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

				equal();

			}
		});
		jbtns[3][3].addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

				opEval(0);
			}
		});
		;

		jlResultado = new JLabel(" - - - - - - ");
		jlResultado.setHorizontalAlignment(SwingConstants.TRAILING);
		jpButtons = new JPanel(new GridLayout(4, 4, 5, 5));

		for (JButton[] jbtn : jbtns) {
			for (int j = 0; j < jbtns[0].length; j++) {
				jpButtons.add(jbtn[j]);
			}
		}

		jbReset = new JButton("Reset");
		jbReset.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

				jlResultado.setText(" - - - - - - ");
				conta = "";
				v1 = "";
				v2 = "";
				op = "";
				result = "";

			}
		});
	}


	private void opEval(int opr) {

		double aux1 = 0, aux2 = 0, res = 0;
		switch (opr) {
			case 0:// +
				op = "+";
				break;
			case 1:// -
				op = "-";
				break;
			case 2:// *
				op = "*";
				break;
			case 3:// /
				op = "/";
				break;
		}
		if (v1.equals("")) {
			v1 = conta;
			conta = "";
			jlResultado.setText(v1);
		} else {
			if (!conta.equals("")) {
				v2 = conta;
			}
			conta = "";
			aux1 = Double.parseDouble(v1);
			aux2 = Double.parseDouble(v2);
			switch (opr) {
				case 0:// +
					res = aux1 + aux2;
					break;
				case 1:// -
					res = aux1 - aux2;
					break;
				case 2:// *
					res = aux1 * aux2;
					break;
				case 3:// /
					try {
						res = aux1 / aux2;
					} catch (Exception e) {
						res = 0;
					}
					break;
			}
			v1 = String.valueOf(res);
			jlResultado.setText(v1);
		}
	}


	private void equal() {

		int aux1 = 0, aux2 = 0, res = 0;
		v2 = conta;

		if (v1.equals("") || v2.equals("")) {
			result = " ";
			jlResultado.setText(result);
		} else {

			if (op.equals("+")) {
				aux1 = Integer.parseInt(v1);
				aux2 = Integer.parseInt(v2);
				res = aux1 + aux2;
				result = String.valueOf(res);
				jlResultado.setText(result);
			} else if (op.equals("-")) {
				aux1 = Integer.parseInt(v1);
				aux2 = Integer.parseInt(v2);
				res = aux1 - aux2;
				result = String.valueOf(res);
				jlResultado.setText(result);
			} else if (op.equals("*")) {
				aux1 = Integer.parseInt(v1);
				aux2 = Integer.parseInt(v2);
				res = aux1 * aux2;
				result = String.valueOf(res);
				jlResultado.setText(result);
			} else if (op.equals("\\")) {
				aux1 = Integer.parseInt(v1);
				aux2 = Integer.parseInt(v2);

				if (aux2 != 0) {
					res = aux1 / aux2;
					result = String.valueOf(res);
				} else {
					result = "ERRO!";
				}

				jlResultado.setText(result);

			}

		}
		v1 = v2 = conta = op = "";
	}


	public boolean isNumber(String s) {

		try {
			Double.parseDouble(s);
			return true;
		} catch (Exception e) {
			return false;
		}
	}


	public static void main(String[] args) {

		new Application();
		ScriptEngineManager m = new ScriptEngineManager();
		for (ScriptEngineFactory factory : m.getEngineFactories()) {
			System.out.println(factory.getEngineName());
		}
// ScriptEngine js = new ScriptEngineManager().getEngineByName("js");
// try {
// System.out
// .println(js
// .eval("function D(x){return ((x*2)+3)-1;} var x=3; x=D(x);"));
// } catch (ScriptException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
	}
}
